$(document).ready(function() {
    /* Demo Start */

    /* jQuery-UI Widgets */

    $(".mws-accordion").accordion();

    $(".mws-tabs").tabs();

    $(".mws-datepicker").datepicker({showOtherMonths: true});

    $(".mws-datepicker-wk").datepicker({showOtherMonths: true, showWeek: true});

    $(".mws-datepicker-mm").datepicker({showOtherMonths: true, numberOfMonths: 3});

    $.datepicker.setDefaults($.datepicker.regional["pt-BR"]);
    $(".mws-dtpicker").datetimepicker();

    $(".mws-tpicker").timepicker({});

    $(".mws-slider").slider({range: "min"});

    $(".mws-progressbar").progressbar({value: 37});

    $(".mws-range-slider").slider({range: true, min: 0, max: 500, values: [75, 300]});

    var availableTags = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
    ];
    $(".mws-autocomplete").autocomplete({
        source: availableTags
    });

    $("#mws-jui-dialog").dialog({
        autoOpen: false,
        title: "jQuery-UI Dialog",
        modal: true,
        width: "640",
        buttons: [{
                text: "Close Dialog",
                click: function() {
                    $(this).dialog("close");
                }}]
    });
    $("#mws-form-dialog").dialog({
        autoOpen: false,
        title: "jQuery-UI Modal Form",
        modal: true,
        width: "640",
        buttons: [{
                text: "Submit",
                click: function() {
                    $(this).find('form#mws-validate').submit();
                }}]
    });
    $("#mws-jui-dialog-btn").bind("click", function(event) {
        $("#mws-jui-dialog").dialog("option", {modal: false}).dialog("open");
        event.preventDefault();
    });
    $("#mws-jui-dialog-mdl-btn").bind("click", function(event) {
        $("#mws-jui-dialog").dialog("option", {modal: true}).dialog("open");
        event.preventDefault();
    });
    $("#mws-form-dialog-mdl-btn").bind("click", function(event) {
        $("#mws-form-dialog").dialog("option", {modal: true}).dialog("open");
        event.preventDefault();
    });

    $(".mws-slider-vertical").slider({
        orientation: "vertical",
        range: "min",
        min: 0,
        max: 100,
        value: 60
    });

    $("#eq > span").each(function() {
        // read initial values from markup and remove that
        var value = parseInt($(this).text(), 10);
        $(this).empty().slider({
            value: value,
            range: "min",
            animate: true,
            orientation: "vertical"
        });
    });

    /* Spinner */

    var itemList = [
        {url: "http://ejohn.org", title: "John Resig"},
        {url: "http://bassistance.de/", title: "J&ouml;rn Zaefferer"},
        {url: "http://snook.ca/jonathan/", title: "Jonathan Snook"},
        {url: "http://rdworth.org/", title: "Richard Worth"},
        {url: "http://www.paulbakaus.com/", title: "Paul Bakaus"},
        {url: "http://www.yehudakatz.com/", title: "Yehuda Katz"},
        {url: "http://www.azarask.in/", title: "Aza Raskin"},
        {url: "http://www.karlswedberg.com/", title: "Karl Swedberg"},
        {url: "http://scottjehl.com/", title: "Scott Jehl"},
        {url: "http://jdsharp.us/", title: "Jonathan Sharp"},
        {url: "http://www.kevinhoyt.org/", title: "Kevin Hoyt"},
        {url: "http://www.codylindley.com/", title: "Cody Lindley"},
        {url: "http://malsup.com/jquery/", title: "Mike Alsup"}
    ];

    var opts = {
        's1': {decimals: 2},
        's2': {stepping: 0.25},
        's3': {currency: '$'},
        's4': {decimals: 2}
    };

    for (var n in opts)
        $("#" + n).spinner();

    /* ColorPicker */

    $(".mws-colorpicker").ColorPicker({
        onSubmit: function(hsb, hex, rgb, el) {
            $(el).val(hex);
            $(el).ColorPickerHide();
        },
        onBeforeShow: function() {
            $(this).ColorPickerSetColor(this.value);
        }
    });

    /* Data Tables */
    $(".mws-datatable-fn").dataTable({
        "bSort": false,
        sPaginationType: "full_numbers",
        "oLanguage": {
            "sProcessing": "Aguarde enquanto os dados são carregados ...",
            "sLengthMenu": "Mostrar _MENU_ registros por pagina",
            "sZeroRecords": "Nenhum registro correspondente ao criterio encontrado",
            "sInfoEmtpy": "Exibindo 0 a 0 de 0 registros",
            "sInfo": "Exibindo de _START_ a _END_ de _TOTAL_ registros",
            "sInfoFiltered": "",
            "sSearch": "Procurar",
            "oPaginate": {
                "sFirst": "Primeiro",
                "sPrevious": "Anterior",
                "sNext": "Próximo",
                "sLast": "Último"
            }
        }
    });

    /* imgAreaSelect */

    $(".mws-crop-target").imgAreaSelect({
        handles: true,
        x1: 32, y1: 32, x2: 132, y2: 132,
        onSelectChange: function(img, selection) {
            $("#crop_x1").val(selection.x1);
            $("#crop_y1").val(selection.y1);
            $("#crop_x2").val(selection.x2);
            $("#crop_y2").val(selection.y2);
        }
    });

    /* Full Calendar */

    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();


    $("#mws-calendar").fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        editable: true,
        events: [
            {
                title: 'All Day Event',
                start: new Date(y, m, 1)
            },
            {
                title: 'Long Event',
                start: new Date(y, m, d - 5),
                end: new Date(y, m, d - 2)
            },
            {
                id: 999,
                title: 'Repeating Event',
                start: new Date(y, m, d - 3, 16, 0),
                allDay: false
            },
            {
                id: 999,
                title: 'Repeating Event',
                start: new Date(y, m, d + 4, 16, 0),
                allDay: false
            },
            {
                title: 'Meeting',
                start: new Date(y, m, d, 10, 30),
                allDay: false
            },
            {
                title: 'Lunch',
                start: new Date(y, m, d, 12, 0),
                end: new Date(y, m, d, 14, 0),
                allDay: false
            },
            {
                title: 'Birthday Party',
                start: new Date(y, m, d + 1, 19, 0),
                end: new Date(y, m, d + 1, 22, 30),
                allDay: false
            },
            {
                title: 'Click for Google',
                start: new Date(y, m, 28),
                end: new Date(y, m, 29),
                url: 'http://google.com/'
            }
        ]
    });

    /* Sourcerer */

    $(".mws-code-html").sourcerer('html');

    /* Validation Plugin */

    $("#mws-validate").validate({
        rules: {
            spinner: {
                required: true,
                max: 5
            }
        },
        invalidHandler: function(form, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                var message = errors == 1
                        ? 'You missed 1 field. It has been highlighted'
                        : 'You missed ' + errors + ' fields. They have been highlighted';
                $("#mws-validate-error").html(message).show();
            } else {
                $("#mws-validate-error").hide();
            }
        }
    });

    /* jGrowl Notifications */

    $("#mws-growl-btn").bind("click", function(event) {
        $.jGrowl("Hello World!", {position: "bottom-right"});
    });

    $("#mws-growl-btn-1").bind("click", function(event) {
        $.jGrowl("A sticky message", {sticky: true, position: "bottom-right"});
    });

    $("#mws-growl-btn-2").bind("click", function(event) {
        $.jGrowl("Message with Header", {header: "Important!", position: "bottom-right"});
    });

    $(this).ajaxStart(function() {
        $("#loading").show();
    });

    $(this).ajaxStop(function() {
        $("#loading").hide();
        __dataTable();
        __clickHref();
    });

    __clickHref();

    // limpa o alerta vermelho em notificações
    $("#load-inactive, #load-chamado").on("click", function() {
        $('span', $(this).parent()).html("");
    });

    // limpa lista de notificações ao clicar fora
    $(":not(#load-inactive)").on('click', function() {
        $("#membros-inativos").fadeOut(100, function() {
            $("#membros-inativos").html("");
        });
    });

    $(":not(#load-chamado)").on('click', function() {
        $("#chamados-abertos").fadeOut(100, function() {
            $("#chamados-abertos").html("");
        });
    });
});
function addNotify(mensg) {
    var atual = "not-" + $("#notific ul li").length;
    $("#notific ul").append("<li class='none notify-top' id='" + atual + "'></li>");
    $('#' + atual).load(BASE_URL + "mensagem/" + mensg, function() {
        $('#' + atual).effect('slide', {direction: "vertical", mode: "show", scale: "content"});
    });

    $(".notify-top").off();
    $(".notify-top").on("click", function() {
        removeNotify($(this).attr("id"));
    });

    setTimeout(function() {
        removeNotify(atual);
    }, 5000);
}

contHideNotif = 0;

function removeNotify(id) {
    if (contHideNotif == 0) {
        $('#' + id).effect('slide', {direction: "vertical", mode: "hide", scale: "content"}, function() {
            $('#' + id).remove();
        });
    }
    contHideNotif = 0;
}

function __dataTable() {
    /* Data Tables */
    $(".mws-datatable-fn").dataTable({
        sPaginationType: "full_numbers",
        "oLanguage": {
            "sProcessing": "Aguarde enquanto os dados são carregados ...",
            "sLengthMenu": "Mostrar _MENU_ registros por pagina",
            "sZeroRecords": "Nenhum registro correspondente ao criterio encontrado",
            "sInfoEmtpy": "Exibindo 0 a 0 de 0 registros",
            "sInfo": "Exibindo de _START_ a _END_ de _TOTAL_ registros",
            "sInfoFiltered": "",
            "sSearch": "Procurar",
            "oPaginate": {
                "sFirst": "Primeiro",
                "sPrevious": "Anterior",
                "sNext": "Próximo",
                "sLast": "Último"
            }
        }
    });
}

function replaceAll(string, token, newtoken) {
    while (string.indexOf(token) != -1) {
        string = string.replace(token, newtoken);
    }
    return string;
}

function __clickHref() {
    $("li .mws-i-24, td .mws-i-24, input.mws-i-24").off();
    $("li .mws-i-24, td .mws-i-24, input.mws-i-24").on("click", function() {
        if (!$(this).hasClass('not-load')) {
            $("#container").load($(this).attr("href"));
            window.history.pushState('Object', '', $(this).attr("href"));

            if ($(this).parent().parent().hasClass('menu')) {
                $('.active').removeClass('active');
                $(this).parent().addClass('active');
            }
            return false;
        }
    });
}

function __validaForm() {
    $(".error").removeClass("error");
    var tempValid = true;

    $(".required").each(function() {
        if ($(this).val() == "") {
            $(this).addClass("error");
            tempValid = false;
        }
    });

    return tempValid;
}